#include <iostream>
#include <unistd.h>
#include <cstring>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/ioctl.h>
using namespace std;

int main()
{
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0)
  {
    cout << strerror(errno) << endl;
    exit(3);
  }
  sockaddr_in serve;
  serve.sin_addr.s_addr = INADDR_ANY;
  serve.sin_family = AF_INET;
  serve.sin_port = htons(8080);
  int ret = bind(sock, (sockaddr *)(&serve), sizeof(serve));
  if (ret < 0)
  {
    cout << strerror(errno) << endl;
    exit(4);
  }
  char buffer[1024];
  sockaddr_in client;
  socklen_t len = sizeof(len);
  recvfrom(sock, buffer, sizeof(buffer), 0, (sockaddr *)(&client), &len);
  cout << "dasdasd" << endl;
  cout << buffer << endl;
  int nread;
  while (true)
  {
    ioctl(sock, FIONREAD, &nread);
    cout << nread << endl;
    sleep(1);
    sendto(sock, buffer, sizeof(buffer), 0, (sockaddr *)(&client), sizeof(client));
    // cout << strerror(errno) << endl;
    if (errno == EWOULDBLOCK || errno == EAGAIN)
      cout << "meiyou" << endl;
    if (errno == ENOBUFS)
      cout << "满了" << endl;
  }
  return 0;
}
